<?php
declare (strict_types = 1);

namespace app\admin\controller;

use app\admin\model\Role;
use app\admin\validate\AdminEdit;
use app\admin\validate\AdminSave;
use think\exception\HttpException;
use think\exception\ValidateException;
use think\facade\View;

class Admin extends Base
{
    /**
     * 管理员列表展示
     * 条件搜索
     * 保留条件分页
     * @param \app\admin\model\Admin $admin
     * @return string
     * @throws \think\db\exception\DbException
     */
    public function adminList(\app\admin\model\Admin $admin)
    {
        $keyword = request()->param('keyword');

        $list = $admin->when($keyword,function ($query,$keyword){
            $query->where('username','like',"%$keyword%");
        })->with('role')->paginate([
            'list_rows' => 10,
            'query' => ['keyword' => $keyword],
        ])->order('role_id');
        View::assign('list',$list);
        return View::fetch('admin/admin',['query'=>['keyword' => $keyword]]);
    }

    /**
     * 管理员添加页面展示
     * @param Role $role
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function adminAdd(Role $role)
    {
        $roles = $role->select();
        View::assign('role',$roles);
        return View::fetch('admin/admin-add');
    }

    /**
     * 管理员信息提交入库保存
     * 独立验证器验证参数
     * @param \app\admin\model\Admin $admin
     * @return \think\response\Json|void
     */
    public function adminSave(\app\admin\model\Admin $admin)
    {
        try {
            $data = request()->all();
            validate(AdminSave::class)->check($data);
            $data['password'] = encryptPassword($data['password']);
            $data['nickname'] = 'pyg_'.rand(1000000000,9999999999);
            $data['create_time'] = time();
            $data['update_time'] = time();
            $result = $admin->insert($data);
            if ($result){
                abort(200,'管理员添加成功');
            }
            abort(2002,'管理员添加失败');
        } catch (HttpException $exception){
            return success($exception->getStatusCode(),$exception->getMessage());
        } catch (ValidateException $exception){
            return success(2001,$exception->getError());
        }
    }

    /**
     * 管理员软删除
     * @param \app\admin\model\Admin $admin
     * @param $id
     * @return \think\response\Json|void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function adminDelete(\app\admin\model\Admin $admin,$id)
    {
        try {
            if (!is_numeric($id)){
                abort(2001,'非法传参');
            }
            $data = $admin->find($id);
            if ($data['status'] == 1){
                abort(2003,'不能删除已启用的管理员信息');
            }
            $data->delete();
            abort(200,'删除成功');
        } catch (HttpException $exception){
            return success($exception->getStatusCode(),$exception->getMessage());
        }
    }

    /**
     * 展示某一用户信息编辑页面
     * @param Role $role
     * @param \app\admin\model\Admin $admin
     * @param $id
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function adminEdit(Role $role,\app\admin\model\Admin $admin,$id)
    {
        $roles = $role->select();
        $data = $admin->with('role')->find($id);
        View::assign('role',$roles);
        View::assign('info',$data);
        return View::fetch('admin/admin-edit');
    }

    /**
     * 管理员信息修改
     * @param \app\admin\model\Admin $admin
     * @return \think\response\Json|void
     */
    public function adminUpdate(\app\admin\model\Admin $admin)
    {
        try {
            $data = request()->all();
            validate(AdminEdit::class)->check($data);
            $data['update_time'] = time();
            $result = $admin->where('id',$data['id'])->update($data);
            if ($result){
                abort(200,'修改成功');
            }
            abort(2002,'修改失败');
        } catch (HttpException $exception){
            return success($exception->getStatusCode(),$exception->getMessage());
        } catch (ValidateException $exception){
            return success(2001,$exception->getError());
        }
    }

    public function adminState(\app\admin\model\Admin $admin,$id)
    {
        try {
            $data = $admin->find($id);
            $result = $admin->where('id',$id)
                ->update([
                    'status' => $data['status'] == 1 ? 0 : 1,
                ]);
            if ($result){
                abort(200,'修改成功');
            }
            abort(2001,'修改失败');
        } catch (HttpException $exception){
            return success($exception->getStatusCode(),$exception->getMessage());
        }
    }
}
